package net.comfycastle.musicplayer;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ListView;

/**
 * 현재 재생 목록을 보여주는 액티비티
 * @author ricale
 *
 */
public class PlaylistActivity extends BaseActivity implements BaseListFragment.OnListItemClickListener {
	///////////
	//////////
	/////////   variable
	
	/** 현재 보여지고 있는 프래그먼트 */
	private BaseListFragment mFMain;
	
	///////////
	//////////
	/////////   lifecycle callback method
	
	/**
	 * 액티비티가 생성될 때의 콜백 메서드
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.playlist);
	    
	    Intent intent = getIntent();
		String action = intent.getAction();
		
		if(action.equals(MPApp.ACT_SHOW_PLAYLIST_INGROUP)) {
			String kind = intent.getStringExtra(MPApp.ITT_GROUPKIND);
			String name = intent.getStringExtra(MPApp.ITT_GROUPNAME);
			mFMain = SongListFragment.newInstance(BaseListFragment.OBJECTID.LIST_INGROUP, kind, name);
		} else { // (action.equals(MPApp.ACT_SHOW_PLAYLIST_INALBUM))
			String  name   = intent.getStringExtra(MPApp.ITT_GROUPNAME);
			String  artist = intent.getStringExtra(MPApp.ITT_TITLEARTIST);
			boolean isAA   = intent.getBooleanExtra(MPApp.ITT_ISALBUMARTIST, false);
			mFMain = InalbumFragment.newInstance(BaseListFragment.OBJECTID.LIST_INALBUM, name, artist, isAA);
		} // end if
		
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.add(R.id.playlist_container_basefragment, mFMain).commit();
	} // end onCreate
	
	///////////
	//////////
	/////////   event listener & callback method
	
	/**
	 * 프래그먼트의 아이템(항목)이 클릭되었을 때의 콜백 메서드
	 */
	public void onItemClick(BaseListFragment.OBJECTID fragmentId, ListView l, View view, int position, long id) {
		Intent intent;

		switch(fragmentId) {
		case LIST_INGROUP:
			intent = new Intent();
			intent.setAction(MPApp.ACT_PLAY);
			
			intent.putExtra(MPApp.ITT_GROUPKIND,  ((SongListFragment)mFMain).getKind());
			intent.putExtra(MPApp.ITT_GROUPNAME,  ((SongListFragment)mFMain).getName());
			intent.putExtra(MPApp.ITT_STARTTRACK, position);
			
			setResult(RESULT_OK, intent);
			finish();
			break;
		case LIST_INALBUM: default:
			intent = new Intent();
			intent.setAction(MPApp.ACT_PLAY);
			
			intent.putExtra(MPApp.ITT_GROUPKIND,     MPApp.TAG_ALBUM);
			intent.putExtra(MPApp.ITT_GROUPNAME,     ((InalbumFragment)mFMain).getName());
			intent.putExtra(MPApp.ITT_TITLEARTIST,   ((InalbumFragment)mFMain).getArtist());
			intent.putExtra(MPApp.ITT_ISALBUMARTIST, ((InalbumFragment)mFMain).getIsAA());
			intent.putExtra(MPApp.ITT_STARTTRACK,    position);
			
			setResult(RESULT_OK, intent);
			finish();
			break;
		} // end switch
	} // end onListFragmentItemClick
} // end PlaylistActivity